import re

def join_section_name(section_name):
    section_name = section_name.lower()
    return "-".join(section_name.split())


def section_formatter(lines):
    accum = []
    for line in lines:
        m = re.search(r'\\section\{(.*)\}\W*', line)

        if(m != None):
            print m.group(1)
            section_name = join_section_name(m.group(1))
            line = "\\section{%s} \\label{section.%s}" % (m.group(1), section_name)

        accum.append(line)

    return accum

def subsection_formatter(lines):
    accum = []
    for line in lines:
        m = re.search(r'\\subsection\{(.*)\}\W*', line)

        if(m != None):
            print m.group(1)
            section_name = join_section_name(m.group(1))
            line = "\\subsection{%s} \\label{subsection.%s}" % (m.group(1), section_name)

        accum.append(line)

    return accum

def subsubsection_formatter(lines):
    accum = []
    for line in lines:
        m = re.search(r'\\subsubsection\{(.*)\}\W*', line)

        if(m != None):
            print m.group(1)
            section_name = join_section_name(m.group(1))
            line = "\\subsubsection{%s} \\label{subsubsection.%s}" % (m.group(1), section_name)

        accum.append(line)

    return accum

def texttt_replacer(lines):
    accum = []
    for line in lines:
        accum.append(re.sub(r'\\texttt\{([^\}]+)\}', '\\lstinline\'\\1\'', line))
    return accum

VERB_START = 1
VERB_END = 2
def verbatim_formatter(lines):
    final_lines = []
    mode_stack = []
    data_stack = []
    for line in lines:
        #print line
        if(re.search(r'.*\\begin\{verbatim\}.*', line)):
            mode_stack.append(VERB_START)
            print 'verb_start'
        elif(re.match(r'.*\\end\{verbatim\}.*', line)):
            mode_stack.append(VERB_END)
            print 'verb_end'
        elif(re.search('^\s+$', line)):
            print 'whitespace:'
            print line
            if(mode_stack == []):
                final_lines.append(line)
        else: #non-whitespace text
            if(len(mode_stack) > 0):
                if(mode_stack[-1] == VERB_END):#push the data out, wrap in verbatim tags, 
                    
                    final_lines.append('\\begin{verbatim}\n')
                    for line in data_stack:
                        final_lines.append(line)
                    final_lines.append('\\end{verbatim}\n\n')
                    print 'Non-whitespace after verbatim:'
                    print line
                    print data_stack
                    data_stack = []
                    mode_stack = []
                else:
                    print 'Non-whitespace between verbatim:'
                    print line
                    data_stack.append(line)
            else:
                final_lines.append(line)
    return final_lines
        
# input = open('test.tex', 'r')
# lines = verbatim_formatter(input.readlines())
# races_out = open('test-new.tex', 'w')
# races_out.writelines(lines)

input = open('parsubs.in.tex', 'r')

#lines = verbatim_formatter(input.readlines())
lines = texttt_replacer(input.readlines())

races_out = open('parsubs_formatted.tex', 'w')
races_out.writelines(lines)
